#ifndef FILEDIRUTILS_H
#define FILEDIRUTILS_H

#include <QDir>
#include <QFileInfoList>
#include <QObject>

class FileDirUtils : public QObject
{
    Q_OBJECT
public:
    explicit FileDirUtils(QObject *parent = nullptr);

    qint64 recursiveFileInfoCount(QFileInfo entry)
    {
        QDir dir(entry.absoluteFilePath());
        QFileInfoList entryInfoList = dir.entryInfoList(QDir::Filter::NoDotAndDotDot | QDir::Filter::AllEntries | QDir::Filter::Hidden);
        qint64 count = 0;
        foreach(auto entry, entryInfoList) {
            if (entry.isDir()) {
                count += recursiveFileInfoCount(entry);
            } else if (entry.isFile()) {
                count ++;
            }
        }
        setCount(count + getCount());
        emit getAsyncFileInfoCountSingal(getCount());
        return count;
    }

    qint64 recursiveFileInfoSize(QFileInfo entry)
    {
        QDir dir(entry.absoluteFilePath());
        QFileInfoList entryInfoList = dir.entryInfoList(QDir::Filter::NoDotAndDotDot | QDir::Filter::AllEntries | QDir::Filter::Hidden);
        qint64 space = 0;
        foreach(auto entry, entryInfoList) {
            if (entry.isDir()) {
                space += recursiveFileInfoSize(entry);
            } else if (entry.isFile()) {
                space += entry.size();
            }
        }
        setSpace(count + getSpace());
        emit getAsyncFileInfoSizeSingal(getSpace());
        return space;
    }

    void getAsyncFileInfoCount() {
        qint64 count = 0;
        count = recursiveFileInfoCount(QFileInfo(dirPath));
        setCount(count);
        emit getAsyncFileInfoCountSingal(count);
        emit finish();
    }

    void getAsyncFileInfoSize() {
        qint64 space = 0;
        space = recursiveFileInfoSize(QFileInfo(dirPath));
        setSpace(space);
        emit getAsyncFileInfoSizeSingal(space);
        emit finish();
    }

    QString getDirPath() const;
    void setDirPath(const QString &value);

    qint64 getCount() const;
    void setCount(const qint64 &value);

    qint64 getSpace() const;
    void setSpace(const qint64 &value);

signals:
    void getAsyncFileInfoCountSingal(qint64 count);
    void getAsyncFileInfoSizeSingal(qint64 space);

    void finish();

public slots:

private:
    QString dirPath;
    qint64 count;
    qint64 space;
};

#endif // FILEDIRUTILS_H
